package com.app.ytong.bean;


import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import androidx.fragment.app.FragmentActivity;

import com.alibaba.fastjson.annotation.JSONField;
import com.app.ytong.basis.BasisActivity;
import com.app.ytong.util.MyUtils;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

import my.DialogUtils;
import my.FileUtils;
import my.MathUtils;
import my.SystemParamsUtils;
import my.TimeUtils;

public class GoodsListBean extends BaseListBeanXF {

    public final   static int  ISGUADAN = 2;   //挂单
    public final   static int  NOTGUADAN = 1;   //正常
    private ArrayList<GoodsListData> list = new ArrayList<GoodsListData>();

    public ArrayList<GoodsListData> getList() {
        return list;
    }

    public void setList(ArrayList<GoodsListData> list) {
        this.list = list;
    }

    @JSONField(serialize = false)
    public void addListBean(GoodsListBean listbean) {
        if (listbean == null)
            return;
        if (this.getCurrent_page() == INIT_PAGE) {
            setList(listbean.getList());
        } else {
            if (list == null)
                list = new ArrayList<GoodsListData>();
            list.addAll(listbean.getList());
        }
        setListBeanData(listbean);
    }

    @JSONField(serialize = false)
    public void deleteCheckList(){
        Iterator<GoodsListData> it = list.iterator();
        while(it.hasNext()){
            GoodsListData bean = it.next();
            if(bean.isSelect()){
                it.remove();
            }
        }
    }

    @JSONField(serialize = false)
    public void guaDanCheckList(){
        for (GoodsListData goodsListData : list){
//            if(goodsListData.isSelect()){
                goodsListData.setPri("0");
                goodsListData.setSelect(false);
                goodsListData.setSty(GoodsListBean.ISGUADAN);
//            }
        }
    }

    @JSONField(serialize = false)
    public void changeBeanByPosition(int position,GoodsListData otherBean){
        GoodsListData goodsListData = list.get(position);
        goodsListData.setGid(otherBean.getGid());
        goodsListData.setGno(otherBean.getGno());
        goodsListData.setGna(otherBean.getGna());
        goodsListData.setPath(otherBean.getPath());
        goodsListData.setLna(otherBean.getLna());
        goodsListData.setLid(otherBean.getLid());
        goodsListData.setTna(otherBean.getTna());
        goodsListData.setTid(otherBean.getTid());
        goodsListData.setYno(otherBean.getYno());
        goodsListData.setYid(otherBean.getYid());
        goodsListData.setYna(otherBean.getYna());
        goodsListData.setGsp(otherBean.getGsp());
        goodsListData.setGun(otherBean.getGun());
    }


    @JSONField(serialize = false)
    public void toComplete(String fsg ,Context context,SupplierListData supplier,TraderListData curJiJiaYuan,String barCode){
        String wno = SystemParamsUtils.getAndroidID(context);
        for (GoodsListData data : list){
            data.setWno(wno);
            if(supplier != null){
                data.setCno(supplier.getCno());
                data.setCna(supplier.getCna());
                data.setCid(supplier.getCid());
                data.setCdno(supplier.getCdno());
            }
            data.setIdt(TimeUtils.getCurrentTimeByFormat("yyyy-MM-dd HH:mm:ss"));
            data.setUno(LoginBean.getInstance().getUno());
            if(curJiJiaYuan != null){
                data.setJno(curJiJiaYuan.getUno());
            }
            data.setFsg(fsg);
            data.setPid(barCode);
            if(TextUtils.isEmpty(data.getPri())){
                data.setPri("0");
            }
            data.setIfs(MyUtils.getHomeSelectId());
        }
    }


    public int haveMoreSelect(){
        int value = 0;
        for (GoodsListData data : list){
            if(data.isSelect()){
                value ++;
            }
        }
        return value;
    }

    public GoodsListData getSelectBean(){
        GoodsListData result = null;
        for (GoodsListData data : list){
            if(data.isSelect()){
                result = data;
                break;
            }
        }
        return result;
    }

    @JSONField(serialize = false)
    public void changPrice(Context context,String price){
        if(list== null || list.size()==0) return;
        GoodsListData goodsData = null;
        switch (haveMoreSelect()){
            case 0:
                goodsData = list.get(list.size()-1);
                break;
            case 1:
                goodsData =  getSelectBean();
                break;
            default:
                DialogUtils.showErrorDialog(context,"输入单价时候只能选择一个！");
                return;
        }


        if(goodsData.isGuadan()){
            DialogUtils.showErrorDialog(context,"挂单无法输入单价！");
        }else {
            goodsData.setPri(price);
        }
    }



    @JSONField(serialize = false)
    public String getAllTotal(){
        String total = "0";
        for (GoodsListData goodsData : list){
            total =  MathUtils.add(total,goodsData.getMoney());
        }
        return total;
    }

    @JSONField(serialize = false)
    public String getAllWeight(){
        String weight = "0";
        for (GoodsListData goodsData : list){
            weight =  MathUtils.add(weight,goodsData.getGwt());
        }
        return weight;
    }

    @JSONField(serialize = false)
    public void setPriceChange(int position, String price){
        if (position > list.size()) return;
        list.get(position).setPri(price);
    }

    @JSONField(serialize = false)
    public String getMoneyByPosition(int position){
        if (position > list.size()) return "0";
        return list.get(position).getMoney();
    }

    @JSONField(serialize = false)
    public boolean haveSelected(){
        int haveSelectNum = 0;
        for (GoodsListData data : list){
            if(data.isSelect()){
                haveSelectNum ++;
            }
        }
        return haveSelectNum == 1;
    }


    @JSONField(serialize = false)
    public void copy(CopyResult result,String weight){
        if (list.size()<1 || list == null){
            result.error("当前无数据，无法复制！");
            return;
        }
        if (!haveSelected()){
            result.error("请选择一个！");
            return;
        }
        try {
            for (GoodsListData data : list){
                if(data.isSelect()){
                    GoodsListData newData = FileUtils.copyByJson(data,GoodsListData.class);
                    newData.setRfid("");
                    newData.setGwt(weight);
                    newData.setPri("");
                    newData.setSelect(false);
                    list.add(newData);
                    result.success();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @JSONField(serialize = false)
    public List<String> getAllNameList(){
        List<String> nameList = new ArrayList<>();
        if (list.size()>0){
            for (GoodsListData data : list){
                nameList.add(data.getGna());
            }
        }
        return nameList;
    }

    @JSONField(serialize = false)
    public List<String> getHowMuchTypeByTid(){
        List<String> tidList = new ArrayList<>();
        for (GoodsListData data : list){
            if(!tidList.contains(data.getTid())){
                tidList.add(data.getTid());
            }
        }
        return tidList;
    }

    @JSONField(serialize = false)
    public String getGwtByTid(String tid){
        String weight = "0";
        for (GoodsListData data : list){
            if(tid.equals(data.getTid())){
                weight = MathUtils.add(weight,data.getGwt());
            }
        }
        return weight;
    }


    @JSONField(serialize = false)
    public String check(SupplierListData supplierData, GoodsTypeListBean goodsTypeListBean){
        StringBuffer str = new StringBuffer();
        List<String> tidList = getHowMuchTypeByTid();
        for (String tid : tidList){
            String goodsGwt = getGwtByTid(tid);
            String tra = supplierData.getTraByTid(tid);
            String editTra = goodsTypeListBean.getEditTraByTid(tid);
            if(MathUtils.compareTo(MathUtils.add(tra,editTra),goodsGwt)== -1){
                str.append(getTnaByTid(tid)+"溯源重量不足\n");
            }
        }
        return str.toString();
    }


    @JSONField(serialize = false)
    public void resetSupplierData(SupplierListData supplierData, GoodsTypeListBean goodsTypeListBean){
        List<String> tidList = getHowMuchTypeByTid();
        for (String tid : tidList){
            String goodsGwt = getGwtByTid(tid);
            String tra = supplierData.getTraByTid(tid);
            String editTra = goodsTypeListBean.getEditTraByTid(tid);
            supplierData.setTraByTid(tid,goodsTypeListBean.getTnaByTid(tid),supplierData.getCid(),MathUtils.subtract(MathUtils.add(tra,editTra),goodsGwt));
        }
    }


    @JSONField(serialize = false)
    public String getTnaByTid(String tid){
        for (GoodsListData data : list){
            if(tid.equals(data.getTid())){
                return data.getTna();
            }
        }
        return "";
    }

    @JSONField(serialize = false)
    public GoodsSubmitBean getSubmit(){
        GoodsSubmitBean bean = new GoodsSubmitBean();
        bean.getInstocks().addAll(list);
        return bean;
    }


    @JSONField(serialize = false)
    public GoodsSubmitBean getSubmitCpmplete(SupplierListData supplierListData){
        GoodsSubmitBean bean = new GoodsSubmitBean();
        bean.getInstocks().addAll(list);
        bean.getTraces().addAll(supplierListData.getTracelist());
        return bean;
    }


    @JSONField(serialize = false)
    public String getCno(){
        if(list.size()<1)return "";
        return list.get(0).getCno();
    }

    @JSONField(serialize = false)
    public String getCna(){
        if(list.size()<1)return "";
        return list.get(0).getCna();
    }

    @JSONField(serialize = false)
    public boolean haveRfid(){
        boolean have = true;
        for (GoodsListData data : list){
            if(TextUtils.isEmpty(data.getRfid())){
                have = false;
            }
        }
        return have;
    }


    @JSONField(serialize = false)
    public boolean haveRepeat(){
        boolean repeat = false;
        List<String> rfid = new ArrayList<>();

        for (GoodsListData data : list){
            if(rfid.contains(data.getRfid())){
                repeat = true;
            }else {
                rfid.add(data.getRfid());
            }
        }
        return repeat;
    }


    @JSONField(serialize = false)
    public String getMid(){
        for (GoodsListData data : list){
            if(!TextUtils.isEmpty(data.getMid())){
                return data.getMid();
            }
        }
        return "0";
    }


    public interface CopyResult{
        public void success();
        public void error(String message);
    }
}
